package com.zerui.manager.app.device.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.zerui.cola.dto.Response;
import com.zerui.manager.device.custom.DeviceCustom;
import com.zerui.manager.device.domainservice.DeviceRepository;
import com.zerui.manager.device.gateway.DeviceGateWay;
import com.zerui.manager.web.cmd.DeviceCurfewOpenOrCloseCmd;
import com.zerui.manager.web.enums.DeviceFromEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author lvfengjun
 * @title: DeviceCurfewTimeCmdExe
 * @description:
 * @date 2023/10/20 16:47
 */
@Component
@CatchAndLog
public class DeviceCurfewOpenOrCloseCmdExe {

	@Resource
	private DeviceRepository deviceRepository;
	@Resource
	private DeviceGateWay deviceGateWay;

	public Response execute(DeviceCurfewOpenOrCloseCmd openOrCloseCmd) {
		List<DeviceCustom> devices = deviceRepository.selectByDeviceIds2(Collections.singleton(openOrCloseCmd.getDeviceId()));
		for (DeviceCustom custom : devices) {
			if (custom.getDeviceFrom().equals(DeviceFromEnum.FROM_IOT.getCode())) {
				//TODO 设备存在需要调用物联网关接口 修改设备状态 可以使用公共方法 暂时不做枚举提调整
				custom.setTimedTaskStatus(openOrCloseCmd.getAccessStatus());
				deviceGateWay.iotDeviceAutoOpenEnable(custom);
			} else if (custom.getDeviceFrom().equals(DeviceFromEnum.FROM_ICC.getCode())) {
				custom.setTimedTaskStatus(openOrCloseCmd.getAccessStatus());
				deviceGateWay.iccDeviceAutoOpenEnable(custom);
			}
		}
		return Response.buildSuccess();
	}

}
